%% segement all the files

FileList = {
   
    'Ext_Annulaire.mat'
    'Ext_Auriculaire.mat'
    'Ext_IMRL.mat'
    'Ext_Index.mat'
    'Ext_Majeur.mat'
    'Ext_Poignet.mat'
    'Ext_Pouce.mat'
    %'Fermeture_Poing.mat'
    
    % prolematic
    'Flex_Annulaire.mat'
    'Flex_Auriculaire.mat'
    'Flex_IMRL.mat'
    'Flex_Index.mat'
    'Flex_Majeur.mat'
    % end of problematic
    
    %'Flex_Poignet.mat'
    %'Flex_Pouce.mat'
    'Incl_Radiale.mat'
    'Incl_Ulnaire.mat'
    'Ouverture_Poing.mat'
    'Pronation.mat'
    'Supination.mat'
    
     %'Fichier_Test.mat'
     %'Cocontraction.mat'
    };

Fs = 2000;

window = Fs;
X = []; % contains the trials from all classes
Y = []; % contains the classification from all classes
%frequencies = {[20 100] [100 200] [200 300] [300 400] [400 500] [500 700]};
%frequencies = {[20 100] [100 250]}; good
frequencies = {[20 100] [100 250]};

Nfreq = length(frequencies);

%FileList = {'Flex_Pouce.mat' 'Ext_Poignet.mat' 'Flex_Poignet.mat'};

for curFileIx = 1:length(FileList)
    %load(['D:/dropbox/Gipsa-work/data_EMG/franck/' FileList{curFileIx}]);
    load(['D:/dropbox/Gipsa-work/data_EMG/marc/' FileList{curFileIx}]);
    
    datar = data(:,1:12);
    
    %[dataf] = filter_emg(datar,Fs);
    %dataf = filter_multiple(datar,Fs);
    dataf= datar;
    
    if (size(data,2) == 13)
        trigger_channel_available = true;
        trigger_channel = data(:,13:13);
    else
        trigger_channel_available = false;
    end;
    
    if (trigger_channel_available)
        [best_index,best_trigger] = generate_trials_postitions_using_continous_trigger_channel(trigger_channel);
    else
        [best_index,best_trigger] = generate_trials_postitions_auto(dataf);
    end;

    %plot_trials(data,FileList{curFileIx},best_trigger);
    
    % cut signal
    trials_per_class = zeros(size(dataf,2),window,length(best_index),Nfreq);
    disp(['Trials: ' int2str(length(best_index)) ' ' FileList{curFileIx}])
    
    %get the data for these triggers
    
    for f=1:Nfreq
    
%       [b a]=butter(3, cell2mat(frequencies(f))/(Fs/2), 'bandpass');
%       current_filtered_data = filtfilt(b,a,datar);
        [b,a] = build_filter('Fs',Fs,'HP',frequencies{f}(1),'LP',frequencies{f}(2));
        current_filtered_data = filtfilt(b,a,datar);
        
        for t=1:length(best_index)
            %calculate a range around the trigger
            range = 100;

            trials_per_class(:,:,t,f) = current_filtered_data(best_index(t)-range:best_index(t)-range+window-1,:)';      
        end;
    end;
    
    X = cat(3,X,trials_per_class); % put all trials together - from all classes/files/movements
    Y = cat(1,Y,ones(length(best_index),1) * curFileIx);
end

%% Classification over X and Y

% covariance estimation
%COV = covariances(X); %must be disabled and moved to mdm_freq 


%shuffuling
NTrial = size(X,3); % trials equals movements
ix = randperm(NTrial);

% permutate the data for classification
%COV = COV(:,:,ix); % the sequence is updated from 1 ... ntrial to a random permutation
X = X(:,:,ix,:);
Ys = Y(ix); % it takes Y in the permutated form

% cross validation
NCV = 10;
% MDM
disp('---------- MDM --------------');
out = cross_valid(X,Ys,NCV,@mdm_freq); % COV is produced from X and Ys is produced from Y
disp('Confusion matrix :');
disp(confusionmat(Ys,out));
disp(['Accuracy : ' num2str(100*mean(Ys==out)) ' %']);